<?php
namespace app\madmin\model;
use think\Model;

/**
 * 菜单模型类
 * @author  黄明杰 vx:HMJ125855
 * @version 20190101
 */
class SystemRuleModel extends Model
{

    protected $name = 'SystemRule';

    /**
     * 数据所有方法
     * @param   Array   $Where  查询条件
     * @param   String  $Field  查询字段
     * @param   Srting  $Order  查询排序
     * @return  Array
     */
    public function mAll($Where = [], $Field = '*', $Order = 'rule_id ASC'){
        $Where['rule_del'] = 1;

        return $this->field($Field)->where($Where)->order($Order)->select();
    }

    /**
     * 数据列表方法
     * @param   Array   $Where  查询条件
     * @param   String  $Field  查询字段
     * @param   Srting  $Order  查询排序
     * @return  Array
     */
    public function mList($Where = [], $Field = '*', $Order = 'rule_id ASC'){
        $Where['rule_del'] = 1;

        return $this->field($Field)->where($Where)->order($Order)->select();
    }

    /**
     * 数据总数方法
     * @param   Array   $Where  查询条件
     * @return  Int
     */
    public function mCount($Where = []){
        $Where['rule_del'] = 1;

        return $this->where($Where)->count();
    }

    /**
     * 数据详情方法
     * @param   Array   $Where  查询条件
     * @param   String  $Field  查询字段
     * @return  Array
     */
    public function mInfo($Where = [], $Field = '*'){
        $Where['rule_del'] = 1;

        return $this->field($Field)->where($Where)->find();
    }

    /**
     * 数据详情方法
     * @param   Array   $Where  查询条件
     * @param   String  $Field  查询字段
     * @return  String
     */
    public function mField($Where, $Field){
        $Where['menu_del'] = 1;

        return $this->where($Where)->value($Field);
    }

    /**
     * 数据新增方法
     * @param   Array   $Param  添加内容
     * @return  Array
     */
    public function mAdd($Param){
        $Code = 2;
        $Time = time();

        try{
            $Param['rule_create'] = $Time;

            $Result = $this->allowField(true)->insert($Param);

            $AddID = $this->getLastInsID();

            if($Result){
                $Code = 1;
                $Msgs = lang('XtTsTjCg');
            }else{
                $Msgs = lang('XtTsTjSb');
            }

            addMemberLog(session('AdminID'), 2, lang('XtCdRzXz', ['m'=>session('AdminUser'), 'd'=>$AddID]), $Code);

            return ['code'=>$Code, 'msgs'=>$Msgs, 'urls'=>url('SystemRule/showList')];
        }catch(PDOException $e){
            return ['code'=>$Code, 'msgs'=>$e->getMessage()];
        }
    }

    /**
     * 数据更新方法
     * @param   Array   $Where  更新条件
     * @param   Array   $Param  更新内容
     * @return  Array
     */
    public function mEdit($Where, $Param){
        $Code = 2;
        $Time = time();

        try{
            $Find = $this->mInfo($Where);

            if(empty($Find)){
                $Msgs = lang('XtCwCxCw');

                return ['code'=>$Code, 'msgs'=>$Msgs];
            }else{
                $Where['rule_del'] = 1;

                $Param['rule_update'] = $Time;

                $Result = $this->allowField(true)->where($Where)->update($Param);
            }

            if($Result){
                $Code = 1;
                $Msgs = lang('XtTsGxCg');
            }else{
                $Msgs = lang('XtTsGxSb');
            }
 
            addMemberLog(session('AdminID'), 3, lang('XtCdRzGx', ['m'=>session('AdminUser'), 'd'=>$Where['rule_id']]), $Code);

            return ['code'=>$Code, 'msgs'=>$Msgs, 'urls'=>url('SystemRule/showList')];
        }catch(PDOException $e){
            return ['code'=>$Code, 'msgs'=>$e->getMessage()];
        }
    }

    /**
     * 数据删除方法
     * @param   Array   $Where  更新条件
     * @return  Array
     */
    public function mDel($Where){
        $Code = 2;
        $Time = time();

        try{
            $Find = $this->mInfo($Where);

            if(empty($Find)){
                $Msgs = lang('XtCwCxCw');

                return ['code'=>$Code, 'msgs'=>$Msgs];
            }else{
                $Where['rule_del'] = 1;

                $Param = [];
                $Param['rule_del'] = 2;
                $Param['rule_update'] = $Time;

                $Result = $this->allowField(true)->where($Where)->update($Param);
            }

            if($Result){
                $Code = 1;
                $Msgs = lang('XtTsScCs');
            }else{
                $Msgs = lang('XtTsScSb');
            }

            addMemberLog(session('AdminID'), 4, lang('XtCdRzSc', ['m'=>session('AdminUser'), 'd'=>$Where['rule_id']]), $Code);
 
            return ['code'=>$Code, 'msgs'=>$Msgs];
        }catch(PDOException $e){
            return ['code'=>$Code, 'msgs'=>$e->getMessage()];
        }
    }

    /**
     * 数据所有方法
     * @param   Array   $Menu   操作数组
     * @return  Array
     */
    public static function mMenu($Menu, $Pid = 0){
        $Array = [];

        foreach($Menu as $k => $v){
            if($v['rule_pid'] == $Pid){
                $Array[$k] = $v;

                $Child = self::mMenu($Menu, $v['rule_id']);

                $Array[$k]['rule_child'] = array_values($Child);
            }
        }

        return $Array;
    }

    /**
     * 数据排序方法
     * @param   Array   $Class  操作数组
     * @return  Array
     */
    public static function mTree($Class, $Line = '┗━', $Pid = 0, $Level = 0, $Left = 50){
        $Array = [];

        foreach($Class as $v){
            if($v['rule_pid'] == $Pid){
                $v['rule_level'] = $Level + 1;
                $v['rule_left'] = $Left + 0;
                $v['rule_line'] = str_repeat($Line, $Level);

                $Array[] = $v;
                $Array = array_merge($Array, self::mTree($Class, $Line, $v['rule_id'], $Level + 1 , $Left + 20));
            }
        }

        return $Array;
    }

}